﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class DDTQ_DAO
    {

        #region 1. Inserting
        public static bool ThemDDTQ(DDTQ_DTO ddtq)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaDD", ddtq.MaDD));
                sqlParams.Add(new SqlParameter("@TenDD", ddtq.TenDD));
                sqlParams.Add(new SqlParameter("@GiaVeThamQuan", ddtq.GiaVeThamQuan));
                sqlParams.Add(new SqlParameter("@ChiPhiAnUong", ddtq.ChiPhiAnUong));
                sqlParams.Add(new SqlParameter("@MaTP", ddtq.MaTP));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertDIA_DIEM_THAM_QUAN", sqlParams);

                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Updating
        public static bool SuaDDTQ(DDTQ_DTO ddtq)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaDD", ddtq.MaDD));
                sqlParams.Add(new SqlParameter("@TenDD", ddtq.TenDD));
                sqlParams.Add(new SqlParameter("@GiaVeThamQuan", ddtq.GiaVeThamQuan));
                sqlParams.Add(new SqlParameter("@ChiPhiAnUong", ddtq.ChiPhiAnUong));
                sqlParams.Add(new SqlParameter("@MaTP", ddtq.MaTP));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateDIA_DIEM_THAM_QUAN", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Deleting
        public static bool XoaDDTQ(int MaDD)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaDD", MaDD));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteDIA_DIEM_THAM_QUAN", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 
        public static List<DDTQ_DTO> SelectDDTQsByAndMaTP(int MaTP)
        {
            List<DDTQ_DTO> list = new List<DDTQ_DTO>();
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTP", MaTP));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectDIA_DIEM_THAM_QUANsByAndMaTP", sqlParams);
                foreach (DataRow dr in dt.Rows)
                {
                    DDTQ_DTO ddtq = new DDTQ_DTO();
                    ddtq.MaDD = int.Parse(dr["MaDD"].ToString());
                    ddtq.TenDD = dr["TenDD"].ToString();
                    ddtq.GiaVeThamQuan = float.Parse(dr["GiaVeThamQuan"].ToString());
                    ddtq.ChiPhiAnUong = float.Parse(dr["ChiPhiAnUong"].ToString());
                    ddtq.MaTP = int.Parse(dr["MaTP"].ToString());
                    list.Add(ddtq);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
         #endregion
    }
}
